/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.upc.dew.cobranzas.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import pe.edu.upc.dew.cobranzas.model.Inquilino;

public class InquilinoDaoImpl implements InquilinoDao {

    private List<Inquilino> inquilinos;

    public InquilinoDaoImpl() {
        this.inquilinos = new ArrayList<Inquilino>();
        this.cargaInquilinos();
    }

    private void cargaInquilinos() {


        Connection connection = null;
        Inquilino inquilino = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            rs = st.executeQuery("select * from T_Inquilino");
            rs.next(); 
           do  {
          
                inquilino = new Inquilino();
                inquilino.setDni(rs.getString("dni"));
                inquilino.setNombre(rs.getString("nombre"));
                inquilino.setApePaterno(rs.getString("apePaterno"));
                inquilino.setApeMaterno(rs.getString("apeMaterno"));
                inquilino.setTelefono(rs.getString("telefono"));
                inquilino.setDireccionRef(rs.getString("direccionRef"));
                this.inquilinos.add(inquilino);

            } while (rs.next());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }


    }

    public Inquilino getInquilinoPorDni(String dni) {
        Inquilino inquilino = new Inquilino();
        for (int i = 1; i <= this.inquilinos.size(); i++) {
            if (this.inquilinos.get(i - 1).getDni().equals(dni)) {
                inquilino = this.inquilinos.get(i - 1);
                break;
            }
        }

        return inquilino;
    }

    public List<Inquilino> Listado() {
        return this.inquilinos;
    }
}
